/*
 * monster.c
 *
 *  Created on: 2013
 *      Author: Eddy Zogbo, Moncef Sbay
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <SDL.h>
#include <SDL_image.h>

#include "../include/game.h"
#include "../include/misc.h"
#include "../include/monster.h"
#include "../include/map.h"

struct s_monster {
	int xm, ym, has_exploded_M;//location and death verification
	SDL_Surface * directed_img[4];
	enum M_way current_way;
	Uint32 timer;

};
// Creates a new monster
t_monster monster_init() {
	t_monster monster = malloc(sizeof(*monster));
	monster->directed_img[WEST_M] = load_image(IMG_MONSTER_LEFT);
	monster->directed_img[EAST_M] = load_image(IMG_MONSTER_RIGHT);
	monster->directed_img[NORTH_M] = load_image(IMG_MONSTER_UP);
	monster->directed_img[SOUTH_M] = load_image(IMG_MONSTER_DOWN);
	monster->timer = SDL_GetTicks();

	monster->current_way = SOUTH_M;
	monster->has_exploded_M = 0;

	return monster;
}
// Initialize the monster and locate the monster
t_monster monster_place(int x, int y) {
	t_monster monster = malloc(sizeof(*monster));
	monster = monster_init(x, y);
	monster->xm = x;
	monster->ym = y;
	return monster;
}

void explode_monster(t_monster monster) {
	assert(monster);
	monster->has_exploded_M = 1;
}

int has_exploded_M(t_monster monster) {
	return monster->has_exploded_M;
}

void monster_kill(int x, int y, t_monster monster) {
	assert(monster);
	if (monster_get_x(monster) == x && monster_get_y(monster) == y) {
		explode_monster(monster);
	}
}


void monster_free(t_monster monster) {
	assert(monster);
	int i;
	for (i = 0; i < 4; i++)
		SDL_FreeSurface(monster->directed_img[i]);

	free(monster);
}

static int monster_move_aux(t_monster monster,t_player player, t_map map, int x1m, int y1m,
		int x2m, int y2m,SDL_Surface * screen) {

	if (!map_is_inside(map, x1m, y1m))
		return 0;

	switch (map_get_cell_type(map, x1m, y1m)) {
	case CELL_SCENERY:
		return 0; //Monster can't go throught trees and stones
		break;

	case CELL_CASE:
		return 0;
		break;
	case CELL_KEY:
		return 0;
		break;
	case CELL_GOAL:
		return 0;
		break;
	case CELL_DOOR:
		return 0;
		break;
	case CELL_CLOSED_DOOR:
		return 0;
		break;
	case CELL_BOMBERWOMAN:
		return 0;
		break;
	case CELL_BONUS:
		return 0;
		break;

	case CELL_PLAYER:
		player_dec_nb_life(screen,player);
		return 1;
		break;

	default:
		break;
	}

	// monster has moved
	return 1;
}

int monster_move(t_monster monster, t_player player, t_map map,
		SDL_Surface * screen) {
	int x = monster->xm;
	int y = monster->ym;
	//player_life_display(x, y, player, screen);
	int move = 0;

	int i;
	i = 1 + rand() % 4;//the monster move randomly
	switch (i) {
	case 1:
		if (monster_move_aux(monster,player, map, x, y - 1, x, y - 2,screen)) {
			monster_set_current_way(monster, NORTH);
			monster->ym--;
			move = 1;
		}
		break;

	case 2:
		if (monster_move_aux(monster,player, map, x, y + 1, x, y + 2,screen)) {
			monster_set_current_way(monster, SOUTH);
			monster->ym++;
			move = 1;
		}
		break;

	case 3:
		if (monster_move_aux(monster,player, map, x - 1, y, x - 2, y,screen)) {
			monster_set_current_way(monster, WEST);
			monster->xm--;
			move = 1;
		}
		break;

	case 4:
		if (monster_move_aux(monster,player, map, x + 1, y, x + 2, y,screen)) {
			monster_set_current_way(monster, EAST);
			monster->xm++;
			move = 1;
		}
		break;
	}

	if (move) {
		map_set_cell_type(map, x, y, CELL_EMPTY);//empty the previous cell
		map_set_cell_type(map, monster->xm, monster->ym, CELL_MONSTER);//the monster own now the cell
	}
	return move;
}

//Display all the monsters on the screen
void monster_display(t_monster monster, t_map map, t_player player,
		SDL_Surface *screen) {

	assert(monster);
	assert(monster->directed_img[monster->current_way]);
	assert(screen);

	SDL_Rect place;
	place.x = monster->xm * SIZE_BLOC;
	place.y = monster->ym * SIZE_BLOC;

	if (!has_exploded_M(monster)) {
		SDL_BlitSurface(monster->directed_img[monster->current_way], NULL,
				screen, &place);
		Uint32 time_display = SDL_GetTicks();
		if ((time_display - monster->timer) > 1000) {
			monster_move(monster, player, map, screen);
			monster->timer = time_display;
		}
	}
}
int monster_get_x(t_monster monster) {
	assert(monster != NULL);
	return monster->xm;
}

int monster_get_y(t_monster monster) {
	assert(monster != NULL);
	return monster->ym;
}

void monster_set_current_way(t_monster monster, enum M_way way) {
	assert(monster);

	monster->current_way = way;
}

